<?php
/**
 * [weliam] Copyright (c) 2016/3/23
 * index.ctrl
 * 全部商品列表控制器
 */
defined('IN_IA') or exit('Access Denied');
$ops = array('select','all');
$op = in_array($_GPC['op'], $ops) ? $_GPC['op'] : '';

$rolearray = array('admin','teacher','student');
global $_W;
if($op == 'select'){
    $arr = $_POST['arr'];
    $device_name = $_POST['device_name'];
    //检查该数组，如果为空，则提示信息
    if(count($arr)==0|$arr==null){
        $data = '您还未选择多选框';
        print_r($data);
        exit;
    }
    //判断当前openid的role是否正确
    $openid = $_W['openid'];
    $current_user = pdo_get('homework_user', array('uniacid'=>$_W['uniacid'],'openid'=>$openid), array('id','name','role')); 
    if($current_user==null){
        $prompt = '您还没有与柜子或柜门进行绑定，无法使用开柜功能！';
        print_r($prompt);
        exit;
    }else{
        if(!in_array($current_user['role'],$rolearray)){
            $prompt = '您的角色无法使用开柜功能！';
            print_r($prompt); 
            exit;        
        }   
    }
    $print_data ='';
    foreach($arr as $key=>$value){
        $open_data = $this->post_open_door($value['info_id'],$value['door']);
        if($current_user['role']==$rolearray[0]|$current_user['role']==$rolearray[1]){
            if($open_data['state']!='success')
                $print_data .= $value['door'].'号门：开启失败';           
            else
                $print_data .= $value['door'].'号门：开启成功';
            $print_data .= '————';            
        }
        if($current_user['role']==$rolearray[2]){
            //学生开柜时，需要向教师推送模板消息
            if($open_data['state']!='success'){
                $print_data .= $value['door'].'号门：开启失败';
            }else{                
                //需要发送模板消息给教师
                $teacher_id = pdo_get('homework_door_teacher', array('uniacid'=>$_W['uniacid'],'device_info_id'=>$value['info_id'],'door'=>$value['door']), array('userid'));
                $teacher_openid = pdo_get('homework_user', array('uniacid' => $_W['uniacid'] ,'id'=>$teacher_id['userid']), array('openid','name'));
                $device_name = pdo_get('homework_cabinet_admin', array('uniacid'=>$_W['uniacid'],'device_info_id'=>$value['info_id']), array('device_name'));
                  
                $account_api = WeAccount::create();
                $token = $account_api->getAccessToken();
                if(is_error($token)){
                    $print_data .= $value['door'].'号门：开启成功。'.'但与该柜门绑定的教师没有收到通知，如有需要请自行微信通知老师';                          
                }
                $url = 'https://api.weixin.qq.com/cgi-bin/message/template/send?access_token='.$token;
                $template_id = '4sT4hto1K27PMwQgodhSsgL_0cDCzrHYRJvd17dlUPg';
                $templateData = array('first'=>array(
                                                'value'=>'您的课代表'.$current_user['name'].'开启了柜门',
                                                'color'=>'#173177'),
                                        'info_fo'=>array(
                                                'value'=>$value['info_id'],
                                                'color'=>'#173177'),
                                        'device_name'=>array(
                                                'value'=>$device_name['device_name'],
                                                'color'=>'#173177'),
                                        'door'=>array(
                                                'value'=>$value['door'],
                                                'color'=>'#173177'),
                                        'name'=>array(
                                                'value'=>$current_user['name'],
                                                'color'=>'#FF0000'),
                                        'remark'=>array(
                                                'value'=>'请'.$teacher_openid['name'].'老师安排时间去作业柜取作业',
                                                'color'=>'#173177'));
                $data = array(
                    'touser'=>$teacher_openid['openid'],
                    'template_id'=>$template_id,
                    'data'=>$templateData
                );
                $re=json_decode($this->postRequest(json_encode($data) , $url),true);
                if($re['errmsg']!='ok')
                    $print_data .= $value['door'].'号门：开启成功！'.'但与该柜门绑定的教师没有收到通知，如有需要请自行微信通知老师';
                else
                    $print_data .= $value['door'].'号门：开启成功';
                
            }            
            $print_data .= '————';            
        }        
        

        
    }
    $print_data = substr($print_data,0,strlen($print_data)-12);
    print_r($print_data);
    exit;    
}
if($op == 'all'){
    $device_name = $_POST['device_name'];
    $info_id = $_POST['info_id'];
    //判断当前openid的role是否正确
    $openid = $_W['openid'];
    $current_user = pdo_get('homework_user', array('uniacid'=>$_W['uniacid'],'openid'=>$openid), array('id','name','role')); 
    if($current_user==null){
        $prompt = '您还没有与柜子或柜门进行绑定，无法使用开柜功能！';
        print_r($prompt);
        exit;
    }else{
        if(!in_array($current_user['role'],$rolearray)){
            $prompt = '您的角色无法使用开柜功能！';
            print_r($prompt); 
            exit;        
        }   
    }
    $return_data = $this->post_open_door($info_id);
    print_r($return_data['text']);
    exit;
}
